package dk.modified.reshary;

import org.apache.http.Header;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import dk.modified.reshary.GlobalState.TrackerName;
import dk.modified.reshary.R.anim;

public class CommentsWriteActivity extends Activity {

	public static final int ACTIVITY_REQUESTCODE = 289532451;

	private Comment comment;
	private int comment_mediaId;
	private int comment_position;
	private EditText edittxt_comment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comments_write);

		Tracker t = ((GlobalState) getApplication())
				.getTracker(TrackerName.APP_TRACKER);
		t.setScreenName("ReShary Comments Write");
		t.send(new HitBuilders.AppViewBuilder().build());

		getActionBar().setDisplayHomeAsUpEnabled(true);
		String title = getResources().getString(
				R.string.title_activity_comments_write);
		Util.setActionBar(this, title);

		edittxt_comment = (EditText) findViewById(R.id.edittxt_comment);

		comment = getIntent().getParcelableExtra("comment");
		comment_position = getIntent().getIntExtra("position", 0);

		if (comment != null) {
			edittxt_comment.setHint(getString(R.string.hint_write_reply));
		} else {
			comment_mediaId = getIntent().getIntExtra("comment_mediaid", 0);
		}

		if (comment_mediaId != 0) {
			Log.d("TEst", comment_mediaId + "");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.writecomment_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			overridePendingTransition(anim.top_in, anim.bottom_out);
			return true;
		}

		if (item.getItemId() == R.id.action_post) {
			String commentText = edittxt_comment.getText().toString().trim();

			if (commentText.length() < 4) {
				Toast.makeText(getApplicationContext(),
						getString(R.string.comment_too_short),
						Toast.LENGTH_LONG).show();
				return true;
			}

			if (comment != null) {
				PostComment(comment.getMediaId(), commentText, comment.getId());
				Intent i = new Intent();
				i.putExtra("position", comment_position);
				setResult(RESULT_OK,i);
				finish();
				overridePendingTransition(anim.top_in, anim.bottom_out);

			} else {
				PostComment(comment_mediaId, commentText, 0);
				setResult(RESULT_OK);
				finish();
				overridePendingTransition(anim.top_in, anim.bottom_out);
			}
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		overridePendingTransition(anim.top_in, anim.bottom_out);
	}

	public void PostComment(int mediaid, String comment, final int child) {
		SharedPreferences sharedpreferences = getSharedPreferences(
				"dk.modified.reshary", Context.MODE_PRIVATE);
		String securitytoken = sharedpreferences.getString("securitytoken", "");
		String userId = sharedpreferences.getString("userid", "");

		RequestParams params = new RequestParams();
		params.put("securitytoken", securitytoken);
		params.put("userid", userId);
		params.put("mediaid", String.valueOf(mediaid));
		params.put("comment", comment);
		if (child != 0) {
			params.put("child", String.valueOf(child));
		}

		ResharyRestClient.post("put_comments/", params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onFailure(int arg0, Header[] arg1, byte[] arg2,
							Throwable arg3) {
						Toast.makeText(getApplicationContext(),
								getString(R.string.comment_failure),
								Toast.LENGTH_LONG).show();
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String content) {
						if (child != 0) {
							Toast.makeText(getApplicationContext(),
									getString(R.string.reply_success),
									Toast.LENGTH_LONG).show();
						} else {
							Toast.makeText(getApplicationContext(),
									getString(R.string.comment_success),
									Toast.LENGTH_LONG).show();
						}
					}

				}, this);
	}

}
